<?php
namespace IKPHP\Common\Models;

use IKPHP\Common\Models\BaseModel;

class UserLevel extends BaseModel
{
	public static $arrRole;
	
	public function initialize(){
		parent::initialize();
		$this->checkCache(); 
	}
    public function getSource()
    {
        return IK."user_role";
    }
    public function getLevelById($roleid){ 
    	$arrRole = self::$arrRole;
    	foreach ($arrRole as $item){
    		if($item['roleid'] == $roleid){
    			return $item;
    		}
    	}
    	return false;
    } 
	//根据用户积分获取用户角色名
	public function getRoleByScore($score){
		$arrRole = self::$arrRole;
		foreach($arrRole as $key=>$item){
			if($score > $item['score_start'] && $score <= $item['score_end'] || $score > $item['score_start'] && $item['score_end']==0 || $score >=0 && $score <= $item['score_end']){
				return $item;
			}
		}
	}
	//写缓存
	private function checkCache(){
		$cacheKey = 'userrole';
        $cache   = self::$modelCache;
        $arrRole = $cache->get($cacheKey);
        if($arrRole == null){
        	$res = self::find(); 
            $cache->save($cacheKey, $res);
            $arrRole = $res; 
        }
        self::$arrRole = $arrRole->toArray();	
	}    
}
